<?php

/**
 * Class Product
 */
class Product extends AppModel {

	/**
	 * Model name
	 *
	 * @type string
	 */
	public $useTable = 'products';

	public $findMethods = ['search' => true];

	public function getCreateData() {
		$count = 20000;
		$sql = '';
		$i = 10000;
		for (; $i < $count; $i++) {
			$uuid = String::uuid();
			$sql .= "INSERT INTO tb_products SET id='{$uuid}',name='name{$i}',description='desc{$i}',";
			$sql .= "detail='{$i}',created=NOW(),modified=NOW() ;";
		}
		$this->query($sql);
	}

	/**
	 * Search Method
	 *
	 * @access protected
	 *
	 * @param       $state before|after
	 * @param       $query Query product conditions
	 * @param array $result Query product information
	 *
	 * @return array
	 */
	protected function _findSearch($state, $query, $result = array()) {
		if ($state === 'before') {
			$searchQuery = Hash::get($query, 'searchQuery');
			$searchConditions = array(
				'or' => array(
					"{$this->alias}.name LIKE" => '%' . $searchQuery . '%',
				)
			);
			$query['conditions'] = array_merge($searchConditions, (array) $query['conditions']);
			return $query;
		}
		return $result;
	}
}
